package convertutil

import (
	"encoding/json"
	"github.com/cihub/seelog"
	"strconv"
)

func ParseInt64(numStr string) int64 {
	val, err := strconv.ParseInt(numStr, 10, 64)
	if err != nil {
		seelog.Errorf("fail to ParseInt64, numStr=%s, err:%v", numStr, err)
	}
	return val
}

func ParseInt(numStr string) int {
	return int(ParseInt64(numStr))
}

func ParseFloat64(numStr string) float64 {
	val, err := strconv.ParseFloat(numStr, 64)
	if err != nil {
		seelog.Errorf("fail to ParseFloat64, numStr=%s, err:%v", numStr, err)
	}
	return val
}

func ParseBool(str string) bool {
	val, err := strconv.ParseBool(str)
	if err != nil {
		seelog.Errorf("fail to ParseBool, str=%s, err:%v", str, err)
	}
	return val
}

func ParseJson(jsonStr string, obj interface{}) bool {
	if jsonStr == "" {
		return false
	}
	err := json.Unmarshal([]byte(jsonStr), obj)
	if err != nil {
		seelog.Errorf("can not json.Unmarshal, jsonData=%s, err:%v", jsonStr, err)
		return false
	}
	// seelog.Debugf("success to ParseJson: %s", jsonStr)
	return true
}

func ToJsonString(obj interface{}) string {
	data, err := json.Marshal(obj)
	if err != nil {
		seelog.Errorf("fail to Marshal obj, obj: %v, err: %v", obj, err)
	}
	return string(data)
}
